//
//  CMThemeService.swift
//  covermedia
//
//  Created by Travel on 16/1/22.
//  Copyright © 2016年 covermedia. All rights reserved.
//

import Foundation
import ChameleonSwift
import Device


open class CMThemeService {
    
    public var readStyle: CMReadStyle = CMReadStyle.day {
        didSet {
            if readStyle != oldValue {
                UIApplication.ch.refresh(with: readStyle)
            }
        }
    }
    
    public static let shared = CMThemeService()
    
    
    public func imageUrl(_ urls:[CMReadStyle: String]) -> String? {
        return urls[self.readStyle]
    }
    
    public func image(_ images:[CMReadStyle: UIImage]) -> UIImage? {
        return images[self.readStyle]
    }
}

public extension UIView {
    public var ts: CMThemeService {
        return CMThemeService.shared
    }
}

public extension UIViewController {
    public var ts: CMThemeService {
        return CMThemeService.shared
    }
}


public extension UIFont {
    
    
    public class func cm_font(_ size: CMFontSize) -> UIFont {
        return UIFont.systemFont(ofSize: size.fontSize)
    }
    
    public class func cm_font(_ Inch4: CMFontSize, Inch4_7: CMFontSize, Inch5: CMFontSize) -> UIFont {
        if Device.isSmallerThanScreenSize(.screen4_7Inch) {
            return UIFont.systemFont(ofSize: Inch4.fontSize)
        } else if Device.isEqualToScreenSize(.screen4_7Inch) {
            return UIFont.systemFont(ofSize: Inch4_7.fontSize)
        } else {
            return UIFont.systemFont(ofSize: Inch5.fontSize)
        }
    }
    
    public class func cm_boldFont(_ size:CMFontSize) -> UIFont {
        return UIFont.boldSystemFont(ofSize: size.fontSize)
    }

    public class func cm_boldFont(_ Inch4: CMFontSize, Inch4_7: CMFontSize, Inch5: CMFontSize) -> UIFont {
        if Device.isSmallerThanScreenSize(.screen4_7Inch) {
            return  UIFont.boldSystemFont(ofSize: Inch4.fontSize)
        } else if Device.isEqualToScreenSize(.screen4_7Inch) {
            return UIFont.boldSystemFont(ofSize: Inch4_7.fontSize)
        } else {
            return UIFont.boldSystemFont(ofSize: Inch5.fontSize)
        }
    }
    
    
    public class func cm_height(_ size: CMFontSize, numberOfLine:Int = 1, lineSpace:CGFloat = 0) -> CGFloat {
        let lineHeight = UIFont.boldSystemFont(ofSize: size.fontSize).lineHeight
        let line = CGFloat(numberOfLine)
        if numberOfLine > 1 {
            return lineSpace > 0 ? ceil(lineHeight * line + lineSpace * line - lineSpace) : ceil(lineHeight * line)
        }
        return ceil(lineHeight)
    }
    
    
    /// 图文混排：统一行图片与文字水平对齐 对于的y位置
    ///
    /// - parameter height: 图片的高度
    ///
    /// - returns: y位置
    public func cm_yPosition(_ height: CGFloat) -> CGFloat {
        let mid = descender + capHeight
        return ceil(descender - height / 2 + mid + 2)
    }
}

public extension UIColor {
    public class func cm_color(_ type:CMThemeWidgetType) -> UIColor {
        return CMThemeSettings.basicColor(CMThemeService.shared.readStyle, basicType: type)
    }
    
    public class func cm_color(_ color: CMCommonColor, alpha: Float = 1.0) -> UIColor {
        return CMThemeSettings.commonColor(color, alpha: alpha)
    }
}


// 支持：主题切换（白天夜间模式）

extension UIView : ChameleonCallBackProtocol {
    open func switchTheme(now: Any, pre: Any?){
        
    }
}

extension UIViewController : ChameleonCallBackProtocol {
    open func switchTheme(now: Any, pre: Any?){
        
    }
}
